<?php

namespace app\ctrl;

use app\model\GroupInfo;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use Psr\Http\Message\ResponseInterface;
use Yurun\Util\YurunHttp\Co\Batch;
use Yurun\Util\HttpRequest;

/**
 * 批量组号，并返回每条结果
 */
class Robotsitemap extends \app\ctrl\BaseController
{

    /**
     * 显示提交域名视图
     */
    public function index()
    {
        // 输出详情html内容
        $view = new \core\lib\View(VIEWS);
        return $view->render('robotsitemap', [
            'v' => time(),
            'url' => env('APP_BASEURL') . 'robotsitemap/exec'
        ]);
    }

    /**
     * 处理批量提交的域名并处理.
     * 逻辑：一次只能一个域名提交
     * 1.获取劫持脚本链接参数：如
     * https://kumbhatholograms.com
     * http://kumbhathelootams.com/csv.php
     * 2.请求sxallsitemap.xml。获取对应一级和二级的组号
     */
    public function exec()
    {
//        $data = [
//            'exectime' => '11.11',
//            'host' => 'proconsumers.org',
//            'robots_content' => "User-agent: *\nAllow: /\nSitemap:https://proconsumers.org/sitemap.xml\nSitemap:https://proconsumers.org/sitemap-index1.xml\nSitemap:https://proconsumers.org/sitemap-index2.xml\nSitemap:https://proconsumers.org/sitemap-index3.xml\nSitemap:https://proconsumers.org/sitemap-index4.xml\nSitemap:https://proconsumers.org/sitemap-index5.xml\nSitemap:https://proconsumers.org/sitemap-index6.xml\nSitemap:https://proconsumers.org/sitemap-index7.xml\nSitemap:https://proconsumers.org/sitemap-index8.xml\nSitemap:https://proconsumers.org/sitemap-index9.xml\nSitemap:https://proconsumers.org/sitemap-index10.xml\nSitemap:https://proconsumers.org/sitemap-index11.xml\nSitemap:https://proconsumers.org/sitemap-index12.xml\nSitemap:https://proconsumers.org/sitemap-index13.xml\nSitemap:https://proconsumers.org/sitemap-index14.xml\nSitemap:https://proconsumers.org/sitemap-index15.xml\nSitemap:https://proconsumers.org/sitemap-index16.xml\nSitemap:https://proconsumers.org/sitemap-index17.xml\nSitemap:https://proconsumers.org/sitemap-index18.xml\nSitemap:https://proconsumers.org/sitemap-index19.xml\nSitemap:https://proconsumers.org/sitemap-index20.xml\nSitemap:https://proconsumers.org/sitemap-index21.xml\nSitemap:https://proconsumers.org/sitemap-index22.xml\nSitemap:https://proconsumers.org/sitemap-index23.xml\nSitemap:https://proconsumers.org/sitemap-index24.xml\nSitemap:https://proconsumers.org/sitemap-index25.xml\nSitemap:https://proconsumers.org/sitemap-index26.xml\nSitemap:https://proconsumers.org/sitemap-index27.xml\nSitemap:https://proconsumers.org/sitemap-index28.xml\nSitemap:https://proconsumers.org/sitemap-index29.xml\nSitemap:https://proconsumers.org/sitemap-index30.xml\nSitemap:https://proconsumers.org/sitemap-index31.xml\nSitemap:https://proconsumers.org/sitemap-index32.xml\nSitemap:https://proconsumers.org/sitemap-index33.xml\nSitemap:https://proconsumers.org/sitemap-index34.xml\nSitemap:https://proconsumers.org/sitemap-index35.xml\nSitemap:https://proconsumers.org/sitemap-index36.xml\nSitemap:https://proconsumers.org/qindex.php/sitemap.xml\nSitemap:https://proconsumers.org/qindex.php/sitemap-index1.xml\nSitemap:https://proconsumers.org/qindex.php/sitemap-index2.xml\nSitemap:https://proconsumers.org/qindex.php/sitemap-index3.xml\nSitemap:https://proconsumers.org/qindex.php/sitemap-index4.xml\nSitemap:https://proconsumers.org/qindex.php/sitemap-index5.xml\nSitemap:https://proconsumers.org/qindex.php/sitemap-index6.xml\nSitemap:https://proconsumers.org/qindex.php/sitemap-index7.xml\nSitemap:https://proconsumers.org/qindex.php/sitemap-index8.xml\nSitemap:https://proconsumers.org/qindex.php/sitemap-index9.xml\nSitemap:https://proconsumers.org/qindex.php/sitemap-index10.xml\nSitemap:https://proconsumers.org/qindex.php/sitemap-index11.xml\nSitemap:https://proconsumers.org/qindex.php/sitemap-index12.xml\nSitemap:https://proconsumers.org/qindex.php/sitemap-index13.xml\nSitemap:https://proconsumers.org/qindex.php/sitemap-index14.xml\nSitemap:https://proconsumers.org/qindex.php/sitemap-index15.xml\nSitemap:https://proconsumers.org/qindex.php/sitemap-index16.xml\nSitemap:https://proconsumers.org/qindex.php/sitemap-index17.xml\nSitemap:https://proconsumers.org/qindex.php/sitemap-index18.xml\nSitemap:https://proconsumers.org/qindex.php/sitemap-index19.xml\nSitemap:https://proconsumers.org/qindex.php/sitemap-index20.xml\nSitemap:https://proconsumers.org/qindex.php/sitemap-index21.xml\nSitemap:https://proconsumers.org/qindex.php/sitemap-index22.xml\nSitemap:https://proconsumers.org/qindex.php/sitemap-index23.xml\nSitemap:https://proconsumers.org/qindex.php/sitemap-index24.xml\nSitemap:https://proconsumers.org/qindex.php/sitemap-index25.xml\nSitemap:https://proconsumers.org/qindex.php/sitemap-index26.xml\nSitemap:https://proconsumers.org/qindex.php/sitemap-index27.xml\nSitemap:https://proconsumers.org/qindex.php/sitemap-index28.xml\nSitemap:https://proconsumers.org/qindex.php/sitemap-index29.xml\nSitemap:https://proconsumers.org/qindex.php/sitemap-index30.xml\nSitemap:https://proconsumers.org/qindex.php/sitemap-index31.xml\nSitemap:https://proconsumers.org/qindex.php/sitemap-index32.xml\nSitemap:https://proconsumers.org/qindex.php/sitemap-index33.xml\nSitemap:https://proconsumers.org/qindex.php/sitemap-index34.xml",
//        ];
//        return $data;

        set_time_limit(0);
        $startTime = microtime(true);
        require_once(VENDOR . '/autoload.php');
        $params = post();
        $this->validate($params, [
            'domains' => 'require',
        ]);

        // 验证域名
        $host = $domains = $groupNos = $contents = [];
        $params['domains'] = array_unique(array_filter(explode("\n", $params['domains'])));
        foreach ($params['domains'] as $url) {
            $kaitou = strtolower(substr($url, 0, 5));
            $url = trim($url);
            if (!str_starts_with($kaitou, 'http')) {
                $url = 'https://' . trim($url);
            }
            $url = rtrim($url, '/');
            $parse = parse_url($url);
            $host[] = $parse['host'];
            $isMas = strpos($url, '.php') === false;
            $subphp = '';
            if (!$isMas) {
                preg_match('/(.*)?\/(.*)?\.php/', $url, $match);
                if (!empty($match[2])) {
                    $subphp = $match[2] . '.php';
                }
            }

            $sxallsitemapUrl = $url . '/sxallsitemap.xml';
            $ret = @file_get_contents($sxallsitemapUrl);
            if ($ret === false) {
                $ret = $this->get($sxallsitemapUrl);
            }
            if ($ret !== false) {
                $ret = explode('|', $ret);
                if (!empty($ret[1])) {
                    $groupNo = $ret[1];
                    $groupNos[] = $groupNo;
                    $domains[] = [
                        'url' => $url,
                        'host' => $parse['host'],
                        // 是否一级
                        'mas' => $isMas,
                        // 二级文件名
                        'subphp' => $subphp,
                        // 对应组号
                        'group_no' => $groupNo
                    ];
                }
            }
        }
        $host = array_unique($host);
        if (count($host) !== 1) {
            error('域名数量只能是一条');
        }
        $host = current($host);
        if (empty($domains)) {
            error('无法访问或劫持已掉');
        }

        $groupModel = new GroupInfo();
        $groupNos = array_unique($groupNos);
        $groups = $groupModel->listAll(['group_no' => $groupNos]);
        if (empty($groups) || count($groups) !== count($groupNos)) {
            error('请技术人员添加组号信息');
        }

        // 找到组号的robots范本内容并替换
        $temp = [];
        foreach ($groups as $item) {
            $robotsTemplateLink = str_replace('index.txt', 'robots.txt', $item['shell_link']);
            $robotsContent = $this->get($robotsTemplateLink);
            if ($robotsContent !== false) {
                $robotsContent = str_replace('{domain}', $host, $robotsContent);
                // $robotsContent = str_replace('http:', 'https:', $robotsContent);
            } else {
                $robotsContent = '';
            }
            $temp[] = $robotsContent;
            $contents[$item['group_no']]['robots'] = ltrim($robotsContent);

            // TODO:这边还是不要请求范本，而是allsitemap.xml更加准确
//            $sitemapTemplateLink = str_replace('index.txt', 'sitemap.xml', $item['shell_link']);
//            $sitemapContent = $this->get($sitemapTemplateLink);
//            if ($sitemapContent !== false) {
//                $sitemapContent = str_replace('{domain}', $parse['host'], $sitemapContent);
//                $sitemapContent = str_replace('http:', 'https:', $sitemapContent);
//                if (str_contains($sitemapContent, '{today}')) {
//                    $today = date('Y-m-d');
//                    $sitemapContent = str_replace('{today}', $today, $sitemapContent);
//                }
//            } else {
//                $sitemapContent = '';
//            }
//            $contents[$item['group_no']]['sitemap'] = ltrim($sitemapContent);
        }
        $temp = array_filter($temp);
        if (empty($temp)) {
            error('请求robots返回时返回空');
        }

        // 按照是否一级排序
        $bool = array_column($domains, 'mas');
        array_multisort($bool, SORT_DESC, $domains);

        // 开始组装Robots.txt的内容
        foreach ($domains as $item) {
            // 一级Robots
            if ($item['mas'] && !empty($contents[$item['group_no']]['robots'])) {
                $data['robots_content'] = $contents[$item['group_no']]['robots'];
            }

            // 追加二级robots
            if (!$item['mas'] && !empty($contents[$item['group_no']]['robots']) && !empty($item['subphp']) && !empty($data['robots_content'])) {
                $robotsContent = str_replace($host, $host . '/' . $item['subphp'], $contents[$item['group_no']]['robots']);
                // 开始拼接二级
                $robotsContent = explode("\n", $robotsContent);
                $robotsContent = array_slice($robotsContent, 2);
                $data['robots_content'] .= "\n" . join("\n", $robotsContent);
            } elseif (!$item['mas'] && !empty($contents[$item['group_no']]['robots']) && !empty($item['subphp'])) {
                $data['robots_content'] = str_replace($host, $host . '/' . $item['subphp'], $contents[$item['group_no']]['robots']);
            }

            // 一级sitemap.xml
//            if ($item['mas'] && !empty($contents[$item['group_no']]['sitemap'])) {
//                $sps[] = [
//                    $contents[$item['group_no']]['sitemap'],
//                    $item['subphp']
//                ];
//            } elseif (!empty($contents[$item['group_no']]['sitemap'])) {
//                $sps[] = [
//                    $contents[$item['group_no']]['sitemap'],
//                    $item['subphp']
//                ];
//            }
        }
//        // 开始组装Sitemap.xml的内容
//        $data['sitemap_content'] = '';
//        if (!empty($sps)) {
//            $data['sitemap_content'] = $this->getXmlStr($host, $sps);
//        }

        $data['host'] = $host;
        $endTime = microtime(true);
        $data['exectime'] = number_format($endTime - $startTime, 2);
        return $data;
    }

    /**
     * 请求
     */
    private function get(string $url, array $params = [])
    {
        $client = new GuzzleClient([
            'base_uri' => $url,
            'verify' => false
        ]);
        $request = $client->getAsync($url, []);
        $promise = $request->then(function (ResponseInterface $request) {
            return $request->getBody()->getContents();
        }, function (\Exception $exception) use ($url, $params) {
            if ($exception instanceof ConnectException) {
                // url无法请求
                return false;
            } elseif ($exception instanceof RequestException) {
                // curl时发生request异常
                return false;
            }
        });
        $data = $promise->wait();
        if (empty($data)) {
            return false;
        }
        return $data;
    }

    /**
     * 获取拼装的sitemapXml内容
     */
    public function getXmlStr(string $host, array $contens)
    {
        $dom = new \DOMDocument();
        $xmlStr = "<?xml version = \"1.0\" encoding = \"UTF-8\"?>\n<sitemapindex xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n";
        foreach ($contens as $item) {
            $dom->loadXML($item[0]);
            $locs = $dom->getElementsByTagName('loc');
            $count = count($locs);
            foreach ($locs as $loc) {
                $xmlurl = $loc->textContent;
                break;
            }
            for ($i = 1; $i <= $count; $i++) {
                $link = preg_replace('/\d+/', $i, $xmlurl);
                $temp = "    <sitemap>\n        <loc>" . $link . "</loc>\n    </sitemap>\n";
                if (!$item[1]) {
                    // 如果是二级文件
                    $temp = str_replace($host, $host . '/' . $item[1], $temp);
                }
                $xmlStr .= $temp;
            }
        }
        $xmlStr .= '</sitemapindex>';

        return $xmlStr;
    }
}